//
//  SettingViewController.m
//  HongFuShop
//
//  Created by chirui on 2017/8/10.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "SettingViewController.h"
#import "FeedBackViewController.h"
#import "ChangePasswordViewController.h"

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

{
    UITableView * _tableView;
    
}

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor =[UIColor groupTableViewBackgroundColor];
    self.navigationItem.titleView=[TopLabel withLabelWithLabelText:@"设置"];
    
    [self creatTableView];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_public_return" target:self action:@selector(back)];
    
    
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64-48) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 120;
   
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * cellId = @"cellId";
    
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellId ];
    if (cell==nil) {
        
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }
    
    NSArray * imageArray = @[@"icon_set_mp",@"icon_set_opinion",@"icon_set_clear"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    cell.textLabel.font =[UIFont systemFontOfSize:15];
    
        if (indexPath.row==2) {
            cell.textLabel.text = @"清除缓存";
            double tmpSize = [[SDImageCache sharedImageCache]getSize]/1024/1024;
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM",tmpSize];
            //            }
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       
      }
     if (indexPath.row==0) {
        cell.textLabel.text = @"修改密码";
     }
    if (indexPath.row==1) {
        
        cell.textLabel.text = @"意见反馈";
    }
    
    cell.imageView.image = ImageName(imageArray[indexPath.row]);
   
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  
        UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 120)];
        view.backgroundColor =[UIColor clearColor];
        
        UIButton * btn =[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(50, 60, WIDTH- 100, 40);
        btn.layer.cornerRadius = 20.f;
        btn.layer.masksToBounds = YES;
        [btn setBackgroundImage:ImageName(@"img_home_nav_bg") forState:UIControlStateNormal];
        [btn setTitle:@"退出登录" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(takeOut:) forControlEvents:UIControlEventTouchUpInside];
        
//        if (verifTokenAndEnter) {
    
            [view addSubview:btn];
            
            
//        }else {
//            
//            [btn removeFromSuperview];
//        }
//        
        
        return view;
        
   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
 
    
    if (indexPath.row==2) {
        
        //清除缓存
        double tmpSize = [[SDImageCache sharedImageCache]getSize]/1024/1024;
        
        UIAlertView * alert =[[UIAlertView alloc]initWithTitle:@"温馨提示" message:[NSString stringWithFormat:@"当前缓存%.2fM",tmpSize] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag = 1000;
        [alert show];
    }
    if (indexPath.row==1) {
        
        FeedBackViewController * feedback =[[FeedBackViewController alloc]init];
        [self.navigationController pushViewController:feedback animated:YES];
     }
    
    if (indexPath.row==0) {
        ChangePasswordViewController * change =[[ChangePasswordViewController alloc]init];
        [self.navigationController pushViewController:change animated:YES];
        
    }
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag == 1000) {
        
        if (buttonIndex==0) {
            NSLog(@"cennel");
        }else {
            
            [[SDImageCache sharedImageCache]clearDisk];
            [_tableView reloadData];
        }
    }
    
}


-(void)takeOut:(UIButton *)btn {
    
    [self logOutNetWorking];
}

// 退出
-(void)logOutNetWorking{
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",NetWorkAddress_api,mySetting_logout_api];
    NSDictionary * parameter = @{@"user_id":USER_DEFAULTGet(@"user_id"),
                                 @"token":USER_DEFAULTGet(@"token")
                                 };
    
    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:parameter  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"code"] intValue]==200) {
            SHuiMBhudTextTimeStr(@"退出成功");
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                
                USER_DEFAULTSet(@"", @"token");
                USER_DEFAULTSet(@"", @"enter");
                USER_DEFAULTSet(@"", @"tokenVerify");
                USER_DEFAULTSet(@"", @"user_id");
                USER_DEFAULTSet(@"", @"phone");
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW , (1 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
                    
                    [self.navigationController popViewControllerAnimated:YES];
                });
            });
            
        } else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"请检查您的网络!");
    }];
}


@end
